package cn.com.huzo.webapp.controller.tech;

import cn.com.huzo.model.entities.tech.ProductBomLinesPictureEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.tech.ProductBomHeaderEntity;
import cn.com.huzo.model.entities.tech.ProductBomLinesEntity;
import cn.com.huzo.model.entities.tech.ProductBomLinesProductColourEntity;
import cn.com.huzo.service.master.VersionTypeManager;
import cn.com.huzo.service.tech.ProductBomLinesPictureManager;
import cn.com.huzo.service.tech.ProductBomHeaderManager;
import cn.com.huzo.service.tech.ProductBomLinesManager;
import cn.com.huzo.service.tech.ProductBomLinesProductColourManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * EBOM(即产品BOM，预测表)控制器
 *
 * @author 吴旭
 */
@Controller
public class ProductBomController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "ProductBomHeaderManager")
    private ProductBomHeaderManager _manager_header;

    @Resource(name = "ProductBomLinesManager")
    private ProductBomLinesManager _manager_lines;

    @Resource(name = "ProductBomLinesProductColourManager")
    private ProductBomLinesProductColourManager _manager_lines_product_colour;

    @Resource(name = "ProductBomLinesPictureManager")
    private ProductBomLinesPictureManager _manager_lines_pic;

    @Resource(name = "VersionTypeManager")
    private VersionTypeManager _manager_version_type;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到EBOM一览画面
     * <p>
     * @return
     */
    @RequestMapping("tech/product_bom/list.html")
    public ModelAndView redirectDesignListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("tech.product_bom.list.def");
        return view;
    }

    /**
     * 跳转EBOM编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param headerId EBOM标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("tech/product_bom/{action}/{headerId}/edit.html")
    public ModelAndView redirectDesignEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("headerId", headerId);
        // 为画面设置变量
        view.addObject("VersionTypeList", _manager_version_type.fetchAll());  // 设置版本修订类型
        String strImageUrl = "http://" + request.getServerName() // 服务器地址  
                + ":"
                + request.getServerPort() // 端口号  
                + "/erpimg";
        view.addObject("strImageUrl", strImageUrl);    // 设置图片显示路径

        view.setViewName("tech.product_bom.edit.def");
        return view;
    }

    /**
     * 弹出EBOM选择画面
     * <p>
     * @return
     */
    @RequestMapping("tech/product_bom/choice.html")
    public ModelAndView redirectProductBomPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.product_bom.choice.def");
        return view;
    }

    /**
     * 编辑EBOM配色用成品色
     * <p>
     * @return
     */
    @RequestMapping("tech/product_bom/lines/product_colour/edit.html")
    public ModelAndView redirectProductBomLinesProductColourPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.product_bom_lines_product_colour.edit.def");
        return view;
    }

    /**
     * 批量更改材料色画面
     * <p>
     * @return
     */
    @RequestMapping("tech/product_bom/lines/material_colour/edit.html")
    public ModelAndView redirectProductBomLinesMaterialColourPopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.product_bom_lines_material_colour.edit.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 根据标识取得EBOM数据。
     *
     * @param criterion EBOM标识、当前用户、流程标识
     * @return EBOM数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/header/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId"));
    }

    /**
     * 删除EBOM信息数据。
     *
     * @param criterion 要删除的数据
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }

    /**
     * 根据条件取得EBOM信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return EBOM信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/product_bom/header/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 新增EBOM信息数据。
     * <p>
     * @param instance 要保存的EBOM信息对象。
     * @return 新增记录ID及相关明细数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/product_bom/header/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> add(@RequestBody final ProductBomHeaderEntity instance) throws Exception {
        return _manager_header.add(instance);
    }

    /**
     * 更新EBOM信息数据。
     * <p>
     * @param instance 要保存的EBOM信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "tech/product_bom/header/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer update(@RequestBody final ProductBomHeaderEntity instance) throws Exception {
        return _manager_header.update(instance);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据主表标识，取得明细表数据。
     *
     * @param headerId 单耗ID
     * @return 明细列表数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/fetchLinesByHeaderId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesEntity> fetchLinesByHeaderId(@RequestBody final Long headerId) throws Exception {
        return _manager_lines.fetchLinesByHeaderId(headerId);
    }

    /**
     * 增加明细数据。
     *
     * @param itemList 标识
     * @return 增加后的单耗明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesEntity> insertLines(@RequestBody final List<ProductBomLinesEntity> itemList) throws Exception {
        return _manager_lines.insertLines(itemList);
    }

    /**
     * 更新明细数据。
     *
     * @param itemList 要保存的数据
     * @return 更新后的单耗明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLines(@RequestBody final List<ProductBomLinesEntity> itemList) throws Exception {
        if (itemList != null && !itemList.isEmpty()) {
            return _manager_lines.batchUpdate(itemList);
        } else {
            return new HashMap();
        }
    }

    /**
     * 根据料号更该明细材料数据。
     *
     * @param criterion 要更新的数据（行标识、料号）
     * @return 更新后的单耗明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/updateLinesByMaterialId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesByMaterialId(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.updateLinesByMaterialId(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "LinesId"),
                MapUtils.getLong(criterion, "MaterialId"),
                MapUtils.getLong(criterion, "UpdatedBy")
        );
    }

    /**
     * 删除明细数据。
     *
     * @param criterion 要删除的明细标识、主表id、删除的明细信息等
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }

    /**
     * 批量删除明细数据。
     *
     * @param itemList 单耗明细标识
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> batchDeleteLines(@RequestBody final List<ProductBomLinesEntity> itemList) throws Exception {
        return _manager_lines.batchDelete(itemList);
    }

    /**
     * 批量清空单耗数据。
     *
     * @param itemList 单耗明细标识
     * @return 清空后单耗明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/batchClearLinesProductBom.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesEntity> batchClearLinesProductBom(@RequestBody final List<ProductBomLinesEntity> itemList) throws Exception {
        if (itemList != null && !itemList.isEmpty()) {
            return _manager_lines.batchClearLinesProductBom(itemList);
        } else {
            return new ArrayList();
        }
    }

    /**
     * 引入类似款号的单耗明细。
     *
     * @param criterion 要引入的单耗
     * @return 引入后的单耗明细
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/importLinesFromOtherBom.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> importLinesFromOtherBom(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_lines.importLinesFromOtherBom(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "ImportedBomHeaderId"),
                MapUtils.getLong(criterion, "CreatedBy"),
                MapUtils.getLong(criterion, "UpdatedBy"));
    }

    /**
     * 从剪切板粘贴数据。
     *
     * @param criterion 剪切板数据
     * @return 粘贴后的单耗明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/paste.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesEntity> insertFromClipboard(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.insertFromClipboard(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "CopiedLinesId"),
                MapUtils.getLong(criterion, "MaterialId"),
                MapUtils.getLong(criterion, "UomId"),
                MapUtils.getString(criterion, "Specification"),
                MapUtils.getInteger(criterion, "OrderNo"),
                MapUtils.getLong(criterion, "CreatedBy"),
                MapUtils.getLong(criterion, "UpdatedBy"));
    }

    /**
     * 更新单耗明细排列顺序。
     *
     * @param itemList 要更新单耗明细列表
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/tech/product_bom/lines/updateLinesOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesOrder(@RequestBody final List<ProductBomLinesEntity> itemList) throws Exception {
        return _manager_lines.updateLinesOrder(itemList);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-成品色以及颜色搭配数据处理部分">
    /**
     * 根据主表标识，取得配色用成品色表数据。
     *
     * @param headerId 单耗ID
     * @return 搭配用成品色
     * @throws Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/product_colour/fetchByHeaderId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesProductColourEntity> fetchLinesProductColourByHeaderId(@RequestBody final Long headerId) throws Exception {
        return _manager_lines_product_colour.fetchByHeaderId(headerId);
    }

    /**
     * 增加配色用成品色。
     *
     * @param itemList 要增加的数据
     * @return 搭配用成品色、单耗明细等
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/product_colour/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> insertProductColour(@RequestBody final List<ProductBomLinesProductColourEntity> itemList) throws Exception {
        return _manager_lines_product_colour.batchInsert(itemList);
    }

    /**
     * 编辑配色的成品色。
     *
     * @param itemList 要编辑的数据
     * @return 搭配用成品色、单耗明细等
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/product_colour/changeProductColour.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> changeProductColour(@RequestBody final List<ProductBomLinesProductColourEntity> itemList) throws Exception {
        return _manager_lines_product_colour.batchUpdate(itemList);
    }

    /**
     * 删除配色用成品色。
     *
     * @param instance 要删除的数据
     * @return 搭配用成品色、单耗明细等
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/product_colour/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteProductColour(@RequestBody final ProductBomLinesProductColourEntity instance) throws Exception {
        return _manager_lines_product_colour.deleteLines(instance);
    }

    /**
     * 拖动行改变成品色显示顺序。
     *
     * @param itemList 要改变的数据
     * @return 搭配用成品色、单耗明细等
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/product_colour/updateLinesOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateProductColourLinesOrder(@RequestBody final List<ProductBomLinesProductColourEntity> itemList) throws Exception {
        return _manager_lines_product_colour.updateProductColourOrder(itemList);
    }

    /**
     * 批量更改材料色。
     *
     * @param itemList 要改变的配色的数据
     * @return 单耗明细等
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/product_bom/lines/material_colour/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ProductBomLinesEntity> batchUpdateMaterialColour(@RequestBody final List<Map<String, Object>> itemList) throws Exception {
        return _manager_lines.batchChangeMaterialColour(itemList);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="图片处理部分">
    /**
     * 保存EBOM图片数据。
     * <p>
     * @param instance 要保存的EBOM图片对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/tech/product_bom/lines/picture/save.html",
            method = RequestMethod.POST,
            produces = {"application/json"})
    public @ResponseBody
    Map<String, Object> savePicture(@RequestBody final ProductBomLinesPictureEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("PictureId", _manager_lines_pic.insert(instance));

        return _result;
    }

    /**
     * 删除EBOM图片数据。
     *
     * @param instance EBOM图片对象
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/tech/product_bom/lines/picture/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer deletePicture(@RequestBody final ProductBomLinesPictureEntity instance) throws Exception {
        return _manager_lines_pic.delete(instance);
    }
    // </editor-fold>
}
